package com.ikkong.biz.meta.intercept;

import com.google.common.collect.BiMap;
import com.google.common.collect.Lists;
import com.ikkong.biz.model.Teacher;
import com.ikkong.biz.service.ClassesService;
import com.ikkong.biz.service.impl.ClassesServiceImpl;
import com.ikkong.core.aop.AopContext;
import com.ikkong.core.dao.Blade;
import com.ikkong.core.dao.Db;
import com.ikkong.core.jfinal.ext.kit.JStrKit;
import com.ikkong.core.jfinal.ext.kit.JsonKit;
import com.ikkong.core.meta.PageIntercept;
import com.ikkong.core.toolbox.Conver;
import com.ikkong.core.toolbox.Record;
import com.ikkong.core.toolbox.kit.CollectionKit;
import com.ikkong.core.toolbox.support.BladePage;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

public class AuthTagIntercept extends PageIntercept {
	/**
	 * 查询后附加字典项
	 */
	@SuppressWarnings("unchecked")
	@Override
	public void queryAfter(AopContext ac) {
		BladePage<Map<String, Object>> page = (BladePage<Map<String, Object>>) ac.getObject();
		List<Map<String, Object>> list = page.getRows();
        for (Map<String, Object> map : list) {
            List<String> query = Lists.newArrayList();
            String teacher_tag = Conver.toStr(map.get("teacher_auth"),"");
            System.out.println(teacher_tag);
            String result = Db.init().queryStr("SELECT  GROUP_CONCAT(t.name) as result FROM tb_teacher t WHERE t.id IN (#{join(teas)})",
                    Record.create().set("teas", JStrKit.removeWrapWithDelimiter(teacher_tag).split(",")));
            System.out.println("2222:"+result);

            String student_tag = Conver.toStr(map.get("student_auth"),"");
            String parent_tag = Conver.toStr(map.get("parent_auth"),"");
            query.addAll(Arrays.asList(JStrKit.removeWrapWithDelimiter(student_tag).split(",")));
            query.addAll(Arrays.asList(JStrKit.removeWrapWithDelimiter(parent_tag).split(",")));
            String st = Db.init().queryStr("SELECT  GROUP_CONCAT(t.fullname) FROM tfw_dept t WHERE t.id IN (#{join(query)})",
                    Record.create().set("query", query));
            System.out.println("111"+st);

            query = Lists.newArrayList();
            if (JStrKit.notBlank(result)){
                query.addAll(Arrays.asList(result.split(",")));
            }
            if (JStrKit.notBlank(st)){
                query.addAll(Arrays.asList(st.split(",")));
            }

            map.put("auth",CollectionKit.join(query,","));
        }
	}

}
